package com.xfapp.demos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xfapp.demos.entity.Chapter;
import com.xfapp.demos.entity.DataStructure;
import com.xfapp.demos.mapper.ChapterMapper;
import com.xfapp.demos.service.IChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jinYi
 * @since 2025-06-04
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements IChapterService {


    @Override
    public DataStructure getDataStructureFromChapterTable(Long chapterId) {
        // 查询所有未删除的章节
        List<Chapter> chapters = list(new LambdaQueryWrapper<Chapter>()
                .eq(Chapter::getDelFlag, 0));

        // 构造 DataStructure
        DataStructure dataStructure = new DataStructure();
        //用于获取tab标签的paneKey
        List<String>  tabKeys = new ArrayList<>();

        // 构建 Tabs
        List<DataStructure.Tab> tabs = chapters.stream()
                .filter(c -> c.getParentId() == -2 || c.getParentId().equals(chapterId))
                .map(c -> {
                    String paneKey = "tab_" + c.getId();
                    tabKeys.add(paneKey); // 将 tab 的标题加入 tabKeys 列表中
                    return new DataStructure.Tab(paneKey, c.getName());
                })
                .collect(Collectors.toList());

        dataStructure.setTabs(tabs);

        // 构建 CellCounts 和 CellValues
        Map<String, Integer> cellCounts = new HashMap<>();
        Map<String, List<String>> cellValues = new HashMap<>();
        chapters.stream()
                .filter(c -> c.getParentId() > 0 && tabs.stream().anyMatch(tab -> tab.getPaneKey().equals("tab_" + c.getParentId())))
                .forEach(c -> {
                    String key = "tab_" + c.getParentId();
                    cellCounts.merge(key, 1, Integer::sum);
                    cellValues.computeIfAbsent(key, k -> new ArrayList<>()).add(c.getName());
                });

        dataStructure.setCellCounts(cellCounts);
        dataStructure.setCellValues(cellValues);

        // 构建 CellSummaries（假设每个二级章节下有三级小结）
        Map<String, List<List<DataStructure.SummaryItem>>> cellSummaries = new HashMap<>();

        // 获取所有二级章节
        List<Chapter> secondaryChapters = chapters.stream()
                .filter(c -> tabKeys.contains("tab_" + c.getParentId()) && c.getParentId() > 0)
                .collect(Collectors.toList());

        // 构建三级小结的二维数组
        secondaryChapters.forEach(sc -> {
            String key = "tab_" + sc.getParentId();

            // 获取当前二级章节下的所有三级小结
            List<DataStructure.SummaryItem> summaries = chapters.stream()
                    .filter(c -> c.getParentId().equals(sc.getId()))
                    .map(c -> new DataStructure.SummaryItem(c.getName(),  String.valueOf(c.getId())))
                    .collect(Collectors.toList());

            if (!summaries.isEmpty()) {
                cellSummaries.computeIfAbsent(key, k -> new ArrayList<>()).add(summaries);
            }
        });

        dataStructure.setCellSummaries(cellSummaries);

        return dataStructure;
    }

}
